﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Model;
using System.Text.RegularExpressions;

namespace FubiManger.UI
{
    partial class EditmanngerForm : Smobiler.Core.Controls.MobileForm
    {
        public EditmanngerForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        ms_merchant merchant = new ms_merchant();
        
        public string stype { get; set; }
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pansave_Press(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtmerchant_name.Text))
            {
                MessageBox.Show("店铺名称不可为空");
            }
            else if (string.IsNullOrEmpty(txtintroduce.Text))
            {
                MessageBox.Show("店铺介绍不可为空");

            }
            else if (string.IsNullOrEmpty(merchant.advertising_map))
            {
                MessageBox.Show("请上传banner图片");
            }
            else if (string.IsNullOrEmpty(merchant.logo))
            {
                MessageBox.Show("请上传logo图片");
            }
            else if (string.IsNullOrEmpty(merchant.cover))
            {
                MessageBox.Show("请上传封面图片");
            }
            else
            {
                merchant.merchant_name = txtmerchant_name.Text;//商户名称
                merchant.introduce = txtintroduce.Text;//商户介绍
                merchant.delivery_price = txtdelivery_price.Text;//起送价
                merchant.distribution_costs = txtdistribution_costs.Text;//配送费
                if (merchantDal.Updateinfo(merchant) > 0)
                {
                    Toast("保存成功");
                }
            }
        }
        private void EditmanngerForm_Load(object sender, EventArgs e)
        {
            merchant = merchantDal.Querybyid(merchantid);
            imgbanner.ResourceID =@"C:\fbimage\"+ merchant.advertising_map;
            imglogo.ResourceID = @"C:\fbimage\" + merchant.logo;
            imgcover.ResourceID = @"C:\fbimage\" + merchant.cover;
            txtintroduce.Text = merchant.introduce;//商户介绍
            txtmerchant_name.Text = merchant.merchant_name;//商户名称
            txtdistribution_costs.Text =Convert.ToDouble( merchant.distribution_costs).ToString("f2");//配送费
            txtdelivery_price.Text = Convert.ToDouble(merchant.delivery_price).ToString("f2");//起送价
        }
        /// <summary>
        /// 账号密码设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panpwd_Press(object sender, EventArgs e)
        {
            EditnameForm frm = new EditnameForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 等级设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panvip_Press(object sender, EventArgs e)
        {
            EditLevelForm frm = new EditLevelForm();
            frm.merchantid = merchantid;//商户ID
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 餐桌设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pandisk_Press(object sender, EventArgs e)
        {
            EditdiskForm frm = new EditdiskForm();
            frm.merchantid = merchantid;//商户ID
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 二维码设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pancode_Press(object sender, EventArgs e)
        {
            MerchantcodeForm frm = new MerchantcodeForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 时间设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panhours_Press(object sender, EventArgs e)
        {
            EditTimeForm frm = new EditTimeForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 设置电话
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panmerchant_tel_Press(object sender, EventArgs e)
        {
            EditTelForm frm = new EditTelForm();
            frm.merchantid = merchantid;//商户ID
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 店铺地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panaddr_Press(object sender, EventArgs e)
        {
            EditaddrForm frm = new EditaddrForm();
            frm.merchantid = merchantid;//商户ID
            this.Form.Show(frm, true);
        }

        private void panbanner_Press(object sender, EventArgs e)
        {
            camera1.GetPhoto();
        }

        private void camera1_ImageCaptured(object sender, BinaryResultArgs e)
        {
            if (e.isError == false)
            {               
                string name = DateTime.Now.ToString("yyyyMMddHHmmssms") + Getcode() + ".png";
                e.SaveFile(name, @"C:\fbimage\");
                merchant.advertising_map = name;//图片名称
                imgbanner.ResourceID = @"C:\fbimage\" + name;//显示图片

            }
        }
        /// <summary>
        /// 随机4位数
        /// </summary>
        /// <returns></returns>
        private int Getcode()
        {
            Random rand = new Random();
            return rand.Next(1000, 9999);
        }
        /// <summary>
        /// 打开logo功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panlogo_Press(object sender, EventArgs e)
        {
            camera2.GetPhoto();
        }
        /// <summary>
        /// 上传logo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void camera2_ImageCaptured(object sender, BinaryResultArgs e)
        {
            if (e.isError == false)
            {
                string name = DateTime.Now.ToString("yyyyMMddHHmmssms") + Getcode() + ".png";
                e.SaveFile(name, @"C:\fbimage\");
                merchant.logo = name;//图片名称
                imglogo.ResourceID = @"C:\fbimage\" + name;//显示图片

            }
        }
        /// <summary>
        /// 打开封面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pancover_Press(object sender, EventArgs e)
        {
            camera3.GetPhoto();
        }
        /// <summary>
        /// 上传封面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void camera3_ImageCaptured(object sender, BinaryResultArgs e)
        {
            if (e.isError == false)
            {
                string name = DateTime.Now.ToString("yyyyMMddHHmmssms") + Getcode() + ".png";
                e.SaveFile(name, @"C:\fbimage\");
                merchant.cover = name;//图片名称
                imgcover.ResourceID = @"C:\fbimage\" + name;//显示图片

            }
        }
        /// <summary>
        /// 员工管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panstaff_Press(object sender, EventArgs e)
        {
            StaffsForm frm = new StaffsForm();
            frm.merchantid = merchantid;//商户ID
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 支付宝付款码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel5_Press(object sender, EventArgs e)
        {
            AplaycodeForm frm = new AplaycodeForm();
            frm.aplycode = merchant.alipay_code;
            frm.mid = merchantid;
            this.Form.Show(frm, true, (obj, args) =>
            {
                merchant = merchantDal.Querybyid(merchantid);
            });
        }
        /// <summary>
        /// 微信二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panwxcode_Press(object sender, EventArgs e)
        {
            WxcodeForm frm = new WxcodeForm();
            frm.aplycode = merchant.wechat_code;
            frm.mid = merchantid;
            this.Form.Show(frm, true, (obj, args) =>
            {
                merchant = merchantDal.Querybyid(merchantid);
            });
        }

        private void txtdelivery_price_TextChanged(object sender, EventArgs e)
        {
            Regex r = new Regex("^[0-9]{1,}$");
            if (!r.IsMatch(txtdelivery_price.Text))
            {
                txtdelivery_price.Text = "";
            }
        }

        private void txtdistribution_costs_TextChanged(object sender, EventArgs e)
        {
            Regex r = new Regex("^[0-9]{1,}$");
            if (!r.IsMatch(txtdistribution_costs.Text))
            {
                txtdistribution_costs.Text = "";
            }
        }

        private void txtdelivery_price_TouchLeave(object sender, EventArgs e)
        {
            txtdelivery_price.Text = Convert.ToInt32(txtdelivery_price.Text).ToString();
        }

        private void txtdistribution_costs_TouchLeave(object sender, EventArgs e)
        {
            txtdistribution_costs.Text = Convert.ToInt32(txtdistribution_costs.Text).ToString();
        }

        private void txtdelivery_price_TouchEnter(object sender, EventArgs e)
        {
            txtdelivery_price.Text = "";
        }

        private void txtdistribution_costs_TouchEnter(object sender, EventArgs e)
        {
            txtdistribution_costs.Text = "";
        }
        /// <summary>
        /// 时间段内
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbk1_CheckedChanged(object sender, EventArgs e)
        {
            if (cbk1.Checked == true)
            {
                cbk2.Checked = false;
                cbk3.Checked = false;
                merchantDal.Updateordering_today(merchantid, "1");//修改状态                
            }
        }
        /// <summary>
        /// 否不支持今日点餐
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbk3_CheckedChanged(object sender, EventArgs e)
        {
            if (cbk3.Checked == true)
            {
                cbk2.Checked = false;
                cbk1.Checked = false;
                merchantDal.Updateordering_today(merchantid, "3");//修改状态                
            }
        }
        /// <summary>
        /// 全天都可以点餐
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbk2_CheckedChanged(object sender, EventArgs e)
        {
            if (cbk2.Checked == true)
            {
                cbk3.Checked = false;
                cbk1.Checked = false;
                merchantDal.Updateordering_today(merchantid, "2");//修改状态                
            }
        }
        /// <summary>
        /// 员工管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnstaff_Press(object sender, EventArgs e)
        {
            StaffsForm frm = new StaffsForm();
            frm.merchantid = merchantid;//商户ID
            this.Form.Show(frm, true);
        }
    }
}